package com.liuda.workbench.service.impl;

import com.liuda.commons.constants.Constants;
import com.liuda.commons.utils.DateUtil;
import com.liuda.commons.utils.UUIDUtil;
import com.liuda.settings.domain.User;
import com.liuda.workbench.domain.*;
import com.liuda.workbench.mapper.*;
import com.liuda.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    ClueMapper clueMapper;
    @Autowired
    ClueRemarkMapper clueRemarkMapper;
    @Autowired
    CARMapper carMapper;
    @Autowired
    ActivityMapper activityMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    CustomerRemarkMapper customerRemarkMapper;
    @Autowired
    ContactMapper contactMapper;
    @Autowired
    ContactRemarkMapper contactRemarkMapper;
    @Autowired
    TransferMapper transferMapper;
    @Autowired
    TransferRemarkMapper transferRemarkMapper;
    @Autowired
    CoARMapper coARMapper;
    @Autowired
    TranHistoryMapper tranHistoryMapper;

    @Override
    public List<Clue> queryClueByConditions(Clue clue,Integer startPage,Integer rowsPerPage) {
        return clueMapper.queryClueByConditions(clue,startPage,rowsPerPage);
    }

    @Override
    public int queryClueCountByCondition(Clue clue) {
        return clueMapper.queryClueCountByCondition(clue);
    }

    @Override
    public int insertClueSelective(Clue clue) {
       return clueMapper.insertSelective(clue);
    }

    @Override
    public Clue queryForDetailById(String id) {
        return clueMapper.queryForDetailById(id);
    }

    @Override
    public Clue queryById(String id) {
        return clueMapper.selectByPrimaryKey(id);
    }

    @Override
    public void convert(Map<String, Object> map, User user) throws Exception{
        String clueId=(String)map.get("clueId");
        //1.将线索相关的数据转换的7张表中
        Clue clue=clueMapper.selectByPrimaryKey(clueId);
        Customer customer=new Customer();
        customer.setId(UUIDUtil.createId());
        customer.setOwner(user.getId());
        customer.setName(clue.getCompany());
        customer.setPhone(clue.getMphone());
        customer.setWebsite(clue.getWebsite());
        customer.setCreateBy(user.getId());
        customer.setCreateTime(DateUtil.formatDateTime(new Date()));
        customer.setContactSummary(clue.getContactSummary());
        customer.setNextContactTime(clue.getNextContactTime());
        customer.setAddress(clue.getAddress());
        customer.setDescription(clue.getDescription());

        Contact contact=new Contact();
        contact.setId(UUIDUtil.createId());
        contact.setOwner(user.getId());
        contact.setSource(clue.getSource());
        contact.setCustomerId(customer.getId());
        contact.setFullname(clue.getFullname());
        contact.setAppellation(clue.getAppellation());
        contact.setEmail(clue.getEmail());
        contact.setMphone(clue.getMphone());
        contact.setJob(clue.getJob());
        contact.setCreateBy(user.getId());
        contact.setCreateTime(DateUtil.formatDateTime(new Date()));
        contact.setContactSummary(clue.getContactSummary());
        contact.setNextContactTime(clue.getNextContactTime());
        contact.setAddress(clue.getAddress());
        contact.setDescription(clue.getDescription());

        List<ClueRemark> clueRemarkList=clueRemarkMapper.queryByClueId(clueId);
        List<ContactRemark> contactRemarkList=new ArrayList<>();
        List<CustomerRemark> customerRemarkList=new ArrayList<>();
        for (ClueRemark clueRemark:clueRemarkList) {
            ContactRemark contactRemark=new ContactRemark();
            contactRemark.setContactsId(contact.getId());
            contactRemark.setCreateBy(user.getId());
            contactRemark.setCreateTime(DateUtil.formatDateTime(new Date()));
            contactRemark.setId(UUIDUtil.createId());
            contactRemark.setNoteContent(clueRemark.getNoteContent());
            contactRemark.setEditFlag(Constants.CLUE_REMARK_EDIT_FLAG_FALSE);
            contactRemarkList.add(contactRemark);

            CustomerRemark customerRemark=new CustomerRemark();
            customerRemark.setCustomerId(customer.getId());
            customerRemark.setCreateBy(user.getId());
            customerRemark.setCreateTime(DateUtil.formatDateTime(new Date()));
            customerRemark.setId(UUIDUtil.createId());
            customerRemark.setNoteContent(clueRemark.getNoteContent());
            customerRemark.setEditFlag(Constants.CLUE_REMARK_EDIT_FLAG_FALSE);
            customerRemarkList.add(customerRemark);
        }
        List<CoAR> coARList=new ArrayList<>();
        List<Activity> activityList=activityMapper.queryActivityByClueId(clueId);
        for (Activity activity : activityList) {
            CoAR coAR=new CoAR();
            coAR.setId(UUIDUtil.createId());
            coAR.setActivityId(activity.getId());
            coAR.setContactsId(contact.getId());
            coARList.add(coAR);
        }
        customerMapper.insertSelective(customer);
        contactMapper.insertSelective(contact);
        if(clueRemarkList!=null && clueRemarkList.size()!=0){
            customerRemarkMapper.insertList(customerRemarkList);
            contactRemarkMapper.insertList(contactRemarkList);
        }
        if(activityList!=null && activityList.size()!=0){
            coARMapper.insertList(coARList);
        }

        if(map.get("isCreateTransfer").equals("true")){
            Transfer transfer=new Transfer();
            transfer.setId(UUIDUtil.createId());
            transfer.setOwner(user.getId());
            transfer.setCreateBy(user.getId());
            transfer.setCreateTime(DateUtil.formatDateTime(new Date()));
            transfer.setCustomerId(customer.getId());
            transfer.setContactsId(contact.getId());
            transfer.setMoney((String)map.get("money"));
            transfer.setName((String)map.get("name"));
            transfer.setExpectedDate((String)map.get("expectedDate"));
            transfer.setStage((String)map.get("stage"));
            transfer.setActivityId((String)map.get("activityId"));
            transferMapper.insertSelective(transfer);
            List<TransferRemark> transferRemarkList=new ArrayList<>();
            //生成一条交易历史
            TranHistory tranHistory=new TranHistory();
            tranHistory.setId(UUIDUtil.createId());
            tranHistory.setTranId(transfer.getId());
            tranHistory.setCreateBy(user.getId());
            tranHistory.setCreateTime(DateUtil.formatDateTime(new Date()));
            tranHistory.setStage(transfer.getStage());
            tranHistory.setMoney(transfer.getMoney());
            tranHistory.setExpectedDate(transfer.getExpectedDate());
            tranHistoryMapper.insertSelective(tranHistory);
            for (ClueRemark clueRemark:clueRemarkList) {
                TransferRemark transferRemark = new TransferRemark();
                transferRemark.setTranId(transfer.getId());
                transferRemark.setCreateBy(user.getId());
                transferRemark.setCreateTime(DateUtil.formatDateTime(new Date()));
                transferRemark.setId(UUIDUtil.createId());
                transferRemark.setNoteContent(clueRemark.getNoteContent());
                transferRemark.setEditFlag(Constants.CLUE_REMARK_EDIT_FLAG_FALSE);
                transferRemarkList.add(transferRemark);
            }
            if(clueRemarkList!=null && clueRemarkList.size()!=0){
                transferRemarkMapper.insertList(transferRemarkList);
            }
        }

        //2.删除该线索相关的数据
        clueRemarkMapper.deleteByClueId(clueId);
        carMapper.deleteByClueId(clueId);
        clueMapper.deleteByPrimaryKey(clueId);
    }

    @Override
    public int updateById(Clue clue) {
        return clueMapper.updateByPrimaryKeySelective(clue);
    }

    @Override
    public int deleteByIds(String[] ids) {
        return clueMapper.deleteByIds(ids);
    }

    @Override
    public List<Count> getClueCountByOwner() {
        return clueMapper.getClueCountByOwner();
    }
}
